﻿using Niti.OrderManage.OrderManage.Enum;
using Niti.OrderManage.OrderManage.ValueObject;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace Niti.OrderManage.OrderManage
{
   public class ReceiptOrder : Entity<int>
    {
        /// <summary>
        /// 采购订单编号
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 入库订单编号
        /// </summary>
        public string ReceiptCode { get; set; }
        /// <summary>
        /// 订单的标题
        /// </summary>

        public string Title { get; set; }
        #region 供应商
        /// <summary>
        /// 供应商编号
        /// </summary>
        public string Supplier_Code { get; set; }
        /// <summary>
        /// 供应商名称
        /// </summary>

        public string Supplier_SName { get; set; }
        #endregion
        /// <summary>
        /// 采购订单商品种类的数量
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 采购订单商品总价
        /// </summary>

        public float TotalPrice { get; set; }
        /// <summary>
        /// 入库订单商品种类的数量
        /// </summary>
        public int RealityCount { get; set; }
        /// <summary>
        /// 入库订单商品总价
        /// </summary>

        public float RealityTotalPrice { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }
        /// <summary>
        /// 审核时间
        /// </summary>
        public DateTime? CheckTime { get; set; }
        /// <summary>
        /// 草稿状态 
        /// </summary>
        public OrderStatusCode IsDraft { get; set; } = OrderStatusCode.Draft;

        /// <summary>
        /// 订单明细
        /// </summary>
        [NotMapped]
        public virtual List<ReceiptOrderLineItem> ReceiptOrderLineItem { get; set; } = new List<ReceiptOrderLineItem>();


        protected ReceiptOrder()
        {

        }

        public ReceiptOrder(string receiptCode, string code,int count,float totalPrice, string title, Order_Supplier supplier)
        {
            Check.NotNull(code, nameof(code));
            Code = code;
            ReceiptCode = receiptCode;
            Title = title;
            Count = count;
            TotalPrice = totalPrice;
            RealityCount = count;
            RealityTotalPrice = totalPrice;
            Supplier_Code = supplier.Code;
            Supplier_SName = supplier.SName;
        }

        //变更单条明细
        public void ChangeItem(int lineNumber,float price, int count, List<ReceiptOrderLineItem> orderLineItem)
        {
            //草稿状态
            if (IsDraft == OrderStatusCode.Draft)
            {
                ReceiptOrderLineItem = orderLineItem;
                var existingLine = ReceiptOrderLineItem.FirstOrDefault(ol => ol.LineNumber == lineNumber);


                if (existingLine == null)
                {
                    throw new ArgumentException(
                         "This Order there is not exist this commodity!"
                     );
                }
                else
                {
                    existingLine.UpdateTime = DateTime.Now;
                    existingLine.ChangeRealitySubtotal(price, count);
                }

                RealityCount = 0;
                RealityTotalPrice = 0;
                foreach (var item in ReceiptOrderLineItem)
                {
                    RealityTotalPrice += item.RealitySubtotal;
                    if (item.RealityCount > 0)
                    {
                        RealityCount += 1;
                    }
                    
                }


            }
        }






    }
}
